<template>
  <d2-container class="only">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="组织管理" name="first">
        <div class="flex_just">
          <div style="width:45%">
            <h5>组织结构</h5>
            <el-card>
              <el-tree
                :data="first_data"
                :props="first_defaultProps"
                default-expand-all
                @node-click="first_handleNodeClick"
              ></el-tree>
            </el-card>
          </div>
          <div style="width:45%">
            <div>
              <el-button
                type="default"
                size="small"
                @click="dialogVisible_add_update(1)"
              >
                <d2-icon name="plus" />添加下级
              </el-button>
              <el-button
                type="default"
                size="small"
                @click="dialogVisible_add_update(2)"
              >
                <d2-icon name="edit" />修改
              </el-button>
              <el-button
                type="default"
                size="small"
                icon="el-icon-delete"
                @click="first_delete"
                >删除</el-button
              >
            </div>
            <h5>详细信息</h5>
            <el-card>
              <el-form :model="firstForm" label-width="80px">
                <el-form-item label="组织名称">
                  <el-input disabled v-model="firstForm.name"></el-input>
                </el-form-item>
                <el-form-item label="主管">
                  <el-input disabled v-model="firstForm.names"></el-input>
                </el-form-item>
                <el-form-item label="电话">
                  <el-input disabled v-model="firstForm.mobile"></el-input>
                </el-form-item>
                <div class="flex_just">
                  <el-form-item label="下级组织" style="width:45%">
                    <el-card>
                      <div
                        style="width:100%"
                        v-for="item in firstForm.sub_depart"
                        :key="item.id"
                      >
                        {{ item.name }}
                      </div>
                    </el-card>
                  </el-form-item>
                  <el-form-item label="所属成员" style="width:45%">
                    <el-card>
                      <div
                        style="width:100%"
                        v-for="ite in firstForm.members"
                        :key="ite.id"
                      >
                        {{ ite.name }}
                      </div>
                    </el-card>
                  </el-form-item>
                </div>
              </el-form>
            </el-card>
          </div>
        </div>
        <el-dialog
          title="组织详细信息"
          :visible.sync="first_dialogVisible_add"
          width="30%"
          :append-to-body="true"
          :before-close="handleClose"
        >
          <el-form
            ref="formdata"
            :model="formdata_dialogVisible"
            label-width="100px"
            :rules="rules"
            label-position="left"
            style="width:90%;margin: 0 auto;"
          >
            <!-- <el-form-item label="上级组织：">
              <el-select
                v-model="formdata_dialogVisible.department"
                disabled
                style="width:100%"
              >
              </el-select>
            </el-form-item> -->
            <el-form-item label="组织名称：">
              <el-input v-model="formdata_dialogVisible.name"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="newConstruction()"
              >保存</el-button
            >
          </span>
        </el-dialog>
      </el-tab-pane>
      <el-tab-pane label="角色管理" name="second">
        <el-card>
          <div class="flex_just">
            <div>
              <el-button
                type="default"
                size="mini"
                @click="second_dialogVisible_add = true"
              >
                <d2-icon name="plus" />新增
              </el-button>
              <el-button type="default" size="mini" @click="lllgaim()">
                <d2-icon name="edit" />修名
              </el-button>
              <el-button
                type="default"
                size="mini"
                icon="el-icon-delete"
                @click="second_delete"
                >删除</el-button
              >
            </div>
            <div>
              <el-button size="mini" @click="authorization">授权</el-button>
            </div>
          </div>
        </el-card>
        <p style="">角色列表</p>
        <el-container>
          <el-aside width="200px">
            <el-card style="margin-bottom: 20px;">
              <el-table
                border
                ref="singleTabless"
                :header-cell-style="{ background: 'rgb(191,191,191' }"
                :data="second_tableData"
                highlight-current-row
                @current-change="second_handleCurrentChange_list"
                style="width: 100%"
              >
                <el-table-column label="角色列表" property="name">
                </el-table-column>
              </el-table>
            </el-card>
            <el-card>
              <el-table
                border
                ref="singleTable"
                :header-cell-style="{ background: 'rgb(191,191,191' }"
                :data="second_userData"
                highlight-current-row
                @current-change="second_handleCurrentChange_user"
                style="width: 100%"
              >
                <el-table-column label="拥有用户" property="name">
                </el-table-column>
              </el-table>
            </el-card>
          </el-aside>
          <el-main>
            <el-card>
              <h5>功能权限</h5>
              <el-row
                style="padding: 15px 0;border-bottom:solid 1px #EBEEF5"
                v-for="(item, index) in second_title_list"
                :key="index"
              >
                <el-col :span="5">
                  <el-checkbox
                    v-model="item.checkAll"
                    :indeterminate="item.isIndeterminate"
                    @change="handleCheckAllChange(index, $event)"
                    >{{ item.menu_title }}</el-checkbox
                  >
                </el-col>
                <el-col :span="19">
                  <el-checkbox
                    v-for="city in item.permission_list"
                    v-model="city.checkAll"
                    :label="city.title"
                    :key="city.id"
                    @change="handleCheckedCitiesChange(index, $event)"
                    >{{ city.title }}</el-checkbox
                  >
                </el-col>
              </el-row>
            </el-card>
          </el-main>
        </el-container>
        <el-dialog
          title="新增"
          :visible.sync="second_dialogVisible_add"
          width="30%"
          :before-close="handleClose"
        >
          <el-form label-width="80px">
            <el-form-item label="角色名称">
              <el-input v-model="second_add_username"></el-input>
            </el-form-item>
            <el-form-item style="margin-left: 0px;">
              功能权限默认为【全部】
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="second_dialogVisible_add = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="second_add">保存</el-button>
          </span>
        </el-dialog>
        <el-dialog
          title="改名"
          :visible.sync="second_dialogVisible_update"
          width="30%"
          :before-close="handleClose"
        >
          <el-form label-width="80px">
            <el-form-item label="角色名称">
              <el-input v-model="second_update_username"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="second_dialogVisible_update = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="second_update">保存</el-button>
          </span>
        </el-dialog>
      </el-tab-pane>
      <el-tab-pane label="用户管理" name="third">
        <div class="mb">
          <el-button type="default" size="mini" @click="dialogVisible = true">
            <d2-icon name="plus" />新增
          </el-button>
          <el-button type="default" size="mini" @click="updateFn">
            <d2-icon name="edit" />修改
          </el-button>
          <el-button
            type="default"
            size="small"
            icon="el-icon-delete"
            @click="deletes"
            >删除</el-button
          >
        </div>

        <el-card shadow="never" class="d2-mb">
          <el-table
            :header-cell-style="{ 'text-align': 'center' }"
            :cell-style="{ 'text-align': 'center' }"
            ref="singleTable"
            :data="tableData"
            highlight-current-row
            @current-change="handleCurrentChange"
            style="width: 100%"
          >
            <el-table-column property="name" label="用户名称"></el-table-column>
            <el-table-column
              property="mobile"
              label="移动电话"
            ></el-table-column>
            <el-table-column
              property="depart.name"
              label="部门"
            ></el-table-column>
            <el-table-column property="role" label="角色">
              <template slot-scope="scope">
                {{ scope.row.role[0].name }}
              </template>
            </el-table-column>
            <el-table-column
              property="email"
              label="电子邮件"
            ></el-table-column>
            <el-table-column property="is_active" label="是否有效">
              <template slot-scope="scope">
                <div>{{ scope.row.is_active ? "是" : "否" }}</div>
              </template>
            </el-table-column>
            <el-table-column property="is_manager" label="是否主管">
              <template slot-scope="scope">
                <div>{{ scope.row.is_manager == 1 ? "是" : "否" }}</div>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @current-change="handlePaginationChange"
            :current-page="formdata_third.p"
            :page-size="formdata_third.pageSize"
            layout="total, prev, pager, next"
            :total="formdata_third.count"
          >
          </el-pagination>
        </el-card>
        <!-- 新增 -->
        <el-dialog
          title="用户信息"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <el-form
            ref="formdata_add"
            :model="formdata_add"
            label-width="80px"
            :rules="rules"
          >
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="formdata_add.name"></el-input>
            </el-form-item>
            <el-form-item label="登陆密码" prop="password">
              <el-input v-model="formdata_add.password"></el-input>
            </el-form-item>
            <el-form-item label="移动电话" prop="mobile">
              <el-input v-model="formdata_add.mobile"></el-input>
            </el-form-item>
            <el-form-item label="部门" prop="depart">
              <el-select v-model="formdata_add.depart" style="width:100%">
                <el-option
                  v-for="items in third_datas.depart"
                  :key="items.id"
                  :label="items.name"
                  :value="items.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="role">
              <el-select v-model="formdata_add_role" style="width:100%">
                <el-option
                  v-for="its in third_datas.role"
                  :key="its.id"
                  :label="its.name"
                  :value="its.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="电子邮件" prop="email">
              <el-input v-model="formdata_add.email"></el-input>
            </el-form-item>
            <el-form-item
              label="是否有效"
              style="margin-bottom: 0px;"
              prop="is_active"
            >
              <el-checkbox v-model="formdata_add.is_active"></el-checkbox>
            </el-form-item>
            <el-form-item label="是否主管">
              <el-checkbox v-model="is_manager"></el-checkbox>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="third_add('formdata_add')"
              >保存</el-button
            >
          </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog
          title="用户信息"
          :visible.sync="update_dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <el-form
            ref="update_currentRow"
            :model="update_currentRow"
            label-width="80px"
            :rules="rules"
          >
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="update_currentRow.name"></el-input>
            </el-form-item>
            <!-- <el-form-item label="登陆密码" prop="password">
              <el-input v-model="update_currentRow.password"></el-input>
            </el-form-item> -->
            <el-form-item label="移动电话" prop="mobile">
              <el-input v-model="update_currentRow.mobile"></el-input>
            </el-form-item>
            <el-form-item label="部门" prop="depart">
              <el-select v-model="update_currentRow.depart.name" :disabled='is_superuser' style="width:100%">
                <el-option
                  v-for="items in third_datas.depart"
                  :key="items.id"
                  :label="items.name"
                  :value="items.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="role">
              <el-select v-model="currentRow_update_role" :disabled='is_superuser' style="width:100%">
                <el-option
                  v-for="its in third_datas.role"
                  :key="its.id"
                  :label="its.name"
                  :value="its.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="电子邮件" prop="email">
              <el-input v-model="currentRow.email"></el-input>
            </el-form-item>
            <el-form-item
              label="是否有效"
              style="margin-bottom: 0px;"
              prop="is_active"
            >
              <el-checkbox v-model="update_currentRow.is_active" :disabled='is_superuser'></el-checkbox>
            </el-form-item>
            <el-form-item label="是否主管" prop="is_manager">
              <el-checkbox v-model="update_currentRow.is_manager" :disabled='is_superuser'></el-checkbox>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="update_dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="update_third_keep('update_currentRow')"
              >保存</el-button
            >
          </span>
        </el-dialog>
      </el-tab-pane>
    </el-tabs>
  </d2-container>
</template>
<script>
import { number } from 'echarts/lib/export';
import {
  roles,
  useRoles,
  role_permissions,
  getDepartments,
  departments,
  getUsers,
  users
} from "../../../api/url";
export default {
  data() {
    // 手机号验证
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|6|7|8][0-9]{9}$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };
    return {
      activeName: "first",
      //第一块
      first_dialogVisible_add: false,
      first_data: [],
      first_defaultProps: {
        children: "sub_depart",
        label: "name"
      },
      firstForm: {
        name: "",
        manager: [],
        names: "",
        mobile: "",
        members: null,
        sub_depart: null
      },
      formdata_dialogVisible: {
        department: "上级",
        leader: "",
        name: "",
        phone: ""
      },
      add_update_states: 0,
      // 第二块
      changdu: 0,
      isIndeterminate: false,
      checkAll: false,
      checkedCities: [],
      checkedCities_group: [],
      menusIds: [],

      select_action_group: [],
      second_dialogVisible_add: false,
      second_dialogVisible_update: false,
      second_add_username: "",
      second_update_username: "",
      second_tableData: [],
      second_title_list: [],
      second_userData: [],
      second_currentRow_list: null,
      second_currentRow_user: null,
      // 第三块
      is_superuser:false,
      update_currentRow:{
        name: "",
         password: "123456",
        mobile: "",
        role: [],
        depart: "",
        email: "",
        is_manager: false,
        is_active: false
      },
      formdata_third: {
        p: 1,
        pageSize: 20,
        count: 0,
         is_page:1
      },
      tableData: [],
      currentRow: {
        name: "",
        password: "123456",
        mobile: "",
        role: [],
        depart: "",
        email: "",
        is_manager: false,
        is_active: false
      },
      dialogVisible: false,
      update_dialogVisible: false,
      third_datas: {
        depart: [],
        role: []
      },
      formdata_add_role: "",
      currentRow_update_role: "",
      formdata_add: {
        operation: "ADD",
        name: "",
        password: "123456",
        mobile: "",
        role: [],
        depart: "",
        email: "",
        is_manager: 0,
        is_active: false
      },
      is_manager:false,
      rules: {
        mobile: [{ required: true, validator: checkPhone, trigger: "blur" }],
      }
    };
  },
  created() {
    this.select_fn_first();
  },
  methods: {
    // 树形菜单扁平化方法
    treeData(tree) {
      var queen = [];
      var out = [];
      queen = queen.concat(tree);
      while (queen.length) {
        var first = queen.shift();
        if (first.sub_depart) {
          queen = queen.concat(first.sub_depart);
          delete first["sub_depart"];
        }
        out.push(first);
      }
      return out;
    },
    select_fn_first() {
      // 组织查询
      getDepartments().then(res => {
        this.first_data = res.result;
      });
    },
    handleClick(tab, event) {
      if (tab.name == "second") {
        this.rolesList();
      } else if (tab.name == "third") {
        this.third_list_yh();
        let llldatas = {
          flag:'1'
        }
        getDepartments(llldatas).then(res => {
          // console.log(res.result)
          // this.third_datas.depart = this.treeData(res.result);
          this.third_datas.depart = res.result
        });
        roles().then(res => {
          this.third_datas.role = res.result;
        });
      }
    },
    //第三块分页
    handlePaginationChange(val) {
      this.formdata_third = val;
      this.third_list_yh();
    },
    // 第一块
    first_handleNodeClick(data) {
      this.firstForm = data;
      if (this.firstForm.manager.length == 0) {
        this.firstForm.manager.push({
          name: "",
          mobile: "",
          pid: ""
        });
      }
      this.firstForm.names = this.firstForm.manager[0].name;
      this.firstForm.mobile = this.firstForm.manager[0].mobile;
    },
    dialogVisible_add_update(index) {
      this.add_update_states = index;
      if (index == 2) {
        if (this.firstForm.name == "") {
          this.$message({
            message: "请先选择组织结构",
            type: "warning"
          });
          return;
        } else {
          this.formdata_dialogVisible.name = this.firstForm.name;
        }
      }
      this.first_dialogVisible_add = true;
    },
    first_delete() {
      // 删除
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var data = {
            operation: "DEL",
            id: this.firstForm.id
          };
          departments(data).then(res => {
            this.select_fn_first();
            this.first_dialogVisible_add = false;
            this.$message({
              message: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    newConstruction() {
      if (this.add_update_states == 1) {
        if (this.formdata_dialogVisible.name == "") {
          this.$message({
            message: "组织名字不能为空",
            type: "warning"
          });
          return;
        }
        var data = {
          operation: "ADD",
          name: this.formdata_dialogVisible.name,
          pid: this.firstForm.id
        };
        //添加下级
        departments(data)
          .then(res => {
            this.select_fn_first();
            this.first_dialogVisible_add = false;
            this.formdata_dialogVisible.name = "";
            this.$message({
              message: res.msg,
              type: "success"
            });
          })
          .catch(err => {
            this.$message.error(err.msg);
          });
      } else {
        //修改
        var data = {
          operation: "MOD",
          name: this.formdata_dialogVisible.name,
          pid: this.firstForm.pid,
          id: this.firstForm.id
        };
        departments(data)
          .then(res => {
            this.select_fn_first();
            this.first_dialogVisible_add = false;
            this.$message({
              message: "修改成功",
              type: "success"
            });
          })
          .catch(err => {
            this.$message.error(err.msg);
          });
      }
    },
    // 第二块
    // 获取角色列表
    rolesList() {
      roles().then(res => {
        this.second_tableData = res.result;
      });
      role_permissions().then(res => {
        this.second_title_list = res.result;
        this.second_title_list.forEach(item => {
          this.$set(item, "checkAll", false);
          this.$set(item, "isIndeterminate", false);
          item.permission_list.forEach(its => {
            this.$set(its, "checkAll", false);
          });
        });
      });
    },
    lllgaim() {
      if (this.second_currentRow_list == null) {
        this.$message({
          message: "请先选择角色",
          type: "warning"
        });
        return;
      }
      this.second_update_username = this.second_currentRow_list.name;
      this.second_dialogVisible_update = true;
    },
    second_add() {
      var data = {
        operation: "ADD",
        name: this.second_add_username,
        permission: []
      };
      useRoles(data)
        .then(res => {
          this.second_dialogVisible_add = false;
          roles().then(res => {
            this.second_tableData = res.result;
            let aas = res.result.length - 1;
            role_permissions().then(res => {
              this.second_title_list = res.result;
              this.second_title_list.forEach(item => {
                this.$set(item, "checkAll", false);
                this.$set(item, "isIndeterminate", false);
                item.permission_list.forEach(its => {
                  this.$set(its, "checkAll", false);
                });
              });
              this.$refs["singleTabless"].setCurrentRow(
                this.second_tableData[aas]
              );
            });
          });
          // this.rolesList()
          // this.$refs['singleTabless'].setCurrentRow(this.second_tableData[0]);
          this.$message({
            message: "保存成功",
            type: "success"
          });
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    second_update() {
      let arrys = [];
      this.second_title_list.forEach(item => {
        item.permission_list.forEach(its => {
          if (its.checkAll) {
            arrys.push(its.id);
          }
        });
      });
      var data = {
        operation: "MOD",
        id: this.second_currentRow_list.id,
        name: this.second_update_username,
        permission: arrys
      };
      useRoles(data)
        .then(res => {
          this.second_dialogVisible_update = false;
          roles().then(res => {
            this.second_tableData = res.result;
            let aas = res.result.length - 1;
            role_permissions().then(res => {
              this.second_title_list = res.result;
              this.second_title_list.forEach(item => {
                this.$set(item, "checkAll", false);
                this.$set(item, "isIndeterminate", false);
                item.permission_list.forEach(its => {
                  this.$set(its, "checkAll", false);
                });
              });
              this.$refs["singleTabless"].setCurrentRow(
                this.second_tableData[aas]
              );
            });
          });
          // this.rolesList();
          this.$message({
            message: res.msg,
            type: "success"
          });
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    second_delete() {
      if (this.second_currentRow_list == null) {
        this.$message({
          message: "请先选择角色",
          type: "warning"
        });
      } else {
        var data = {
          operation: "DEL",
          id: this.second_currentRow_list.id
        };
        useRoles(data)
          .then(res => {
            this.second_currentRow_list = null;
            this.$message({
              message: res.msg,
              type: "success"
            });
            this.rolesList();
          })
          .catch(err => {
            this.$message.error(err.msg);
          });
      }
    },
    authorization() {
      if (this.second_currentRow_list == null) {
        this.$message({
          message: "请先选择角色",
          type: "warning"
        });
        return;
      }
      let arrys = [];
      this.second_title_list.forEach(item => {
        item.permission_list.forEach(its => {
          if (its.checkAll) {
            arrys.push(its.id);
          }
        });
      });
      var data = {
        operation: "MOD",
        id: this.second_currentRow_list.id,
        name: this.second_currentRow_list.name,
        permission: arrys
      };
      useRoles(data)
        .then(res => {
          this.$message({
            message: res.msg,
            type: "success"
          });
          roles().then(res => {
            this.second_tableData = res.result;
            var indexssss = this.second_tableData
              .map(item => item.id)
              .indexOf(this.second_currentRow_list.id);
            role_permissions().then(res => {
              this.second_title_list = res.result;
              this.second_title_list.forEach(item => {
                this.$set(item, "checkAll", false);
                this.$set(item, "isIndeterminate", false);
                item.permission_list.forEach(its => {
                  this.$set(its, "checkAll", false);
                });
              });
              this.$refs["singleTabless"].setCurrentRow(
                this.second_tableData[indexssss]
              );
            });
          });
          // this.rolesList();
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    second_handleCurrentChange_list(val) {
      //每次转换先清空
      // console.log(this.second_title_list)
      this.second_title_list.forEach(iss => {
        iss.isIndeterminate = false;
        iss.checkAll = false;
        iss.permission_list.forEach(lss => {
          lss.checkAll = false;
        });
      });

      this.second_title_list.forEach(item => {
        var inds = 0;
        item.permission_list.forEach(ctes => {
          val.permission.forEach(btem => {
            if (ctes.id == btem) {
              ctes.checkAll = true;
            }
          });
          //需判断什么时候半选什么时候全选
          if (ctes.checkAll == true) {
            inds++;
          }
        });
        //需判断什么时候半选什么时候全选
        if (inds == item.permission_list.length) {
          item.checkAll = true;
        } else if (inds == 0) {
          item.checkAll = false;
        } else {
          item.isIndeterminate = true;
        }
      });
      this.second_currentRow_list = val;
      var data = {
        role_id: val.id
      };
      getUsers(data).then(res => {
        this.second_userData = res.result;
      });
    },
    second_handleCurrentChange_user(val) {
      this.second_currentRow_user = val;
    },
    handleCheckedCitiesChange(index, val) {
      if (this.second_currentRow_list == null) {
        this.$message({
          message: "请先选择角色",
          type: "warning"
        });
        this.second_title_list[index].permission_list.forEach(item => {
          item.checkAll = false;
        });
        return;
      }
      let indexss = 0;
      for (
        var i = 0;
        i < this.second_title_list[index].permission_list.length;
        i++
      ) {
        if (this.second_title_list[index].permission_list[i].checkAll) {
          indexss++;
        }
      }
      if (indexss == 0) {
        this.second_title_list[index].isIndeterminate = false;
      } else {
        this.second_title_list[index].isIndeterminate = true;
      }
      if (indexss == this.second_title_list[index].permission_list.length) {
        this.second_title_list[index].isIndeterminate = false;
        this.second_title_list[index].checkAll = true;
      }
    },
    handleCheckAllChange(index, val) {
      if (this.second_currentRow_list == null) {
        this.$message({
          message: "请先选择角色",
          type: "warning"
        });
        this.second_title_list[index].checkAll = false;
        return;
      }
      this.second_title_list[index].permission_list.forEach(item => {
        item.checkAll = val;
      });
    },
    //第三块
    //获取第三块表格数据
    third_list() {
      getUsers().then(res => {
        this.tableData = res.result;
      });
    },
    third_list_yh() {
      getUsers(this.formdata_third).then(res => {
        this.tableData = res.result.results;
        this.formdata_third.count = res.result.count;
        this.tableData.forEach(item =>{
          if(item.depart==null) {
            item.depart={
              name:''
            }
          }
        })
      });
    },
    //表格高亮数据
    handleCurrentChange(val) {
      if(val===null){
        this.currentRow= {
        name: "",
        password: "123456",
        mobile: "",
        role: [],
        depart: "",
        email: "",
        is_manager: false,
        is_active: false
      }
      }else{
        this.currentRow = val;
      }
      if(val===null){
        this.is_superuser=false
      }else{
        this.is_superuser=val.creator.is_superuser
      }
    },
    //修改用户管理
    updateFn(formName) {
      if (this.currentRow == null) {
        this.$message({
          message: "请先选择要修改的用户",
          type: "warning"
        });
        return;
      }
      this.update_currentRow=JSON.parse(JSON.stringify(this.currentRow));
      this.update_currentRow.password = 123456;
      if (this.update_currentRow.name === "") {
        this.$message({
          message: "请先选择要修改的用户",
          type: "warning"
        });
      } else {
        if(this.update_currentRow.depart==null){
          this.update_currentRow.depart={
            name:''
          }
        }
        this.update_dialogVisible = true;
        this.update_currentRow.is_manager =
        this.update_currentRow.is_manager == 1 ? true : false;
        this.currentRow_update_role = this.update_currentRow.role[0].name;
      }
    },
    update_third_keep(currentRow) {
      this.$refs[currentRow].validate(valid => {
        if (valid) {
          let datas =this.update_currentRow
          // datas.depart=datas.depart.id
          // this.
          datas.operation='MOD'
          let lt = [];
          this.third_datas.role.forEach(item => {
            if (item.name == this.currentRow_update_role) {
              this.currentRow_update_role = item.id;
            }
          });
          lt.push(this.currentRow_update_role);
           datas.role = lt;
           datas.is_manager = datas.is_manager ? 1 : 0;
           if(typeof(datas.depart.name)==="number"){
             datas.depart=datas.depart.name
           }else{
            this.third_datas.depart.forEach(iitems=>{
               if(iitems.name==datas.depart.name){
                 datas.depart=iitems.id
               }
             })
           }
          //  console.log(datas)
          // this.update_currentRow.role = lt;
          // this.update_currentRow.is_manager = this.update_currentRow.is_manager ? 1 : 0;
          // this.update_currentRow.operation = "MOD";
          // this.update_currentRow.depart=this.update_currentRow.depart.id
          // console.log(this.currentRow)
          users(datas)
            .then(res => {
              this.currentRow_update_role = "";
              this.third_list();
              // this.$refs.currentRow.resetFields();
              this.update_dialogVisible = false;
              this.$message({
                type: "success",
                message: res.msg
              });
            })
            .catch(err => {  
              this.update_currentRow.depart={
                name:this.update_currentRow.depart
              }
              this.$message.error(err.msg);
            });
        } else {
          this.$message.error("必要参数不可为空");
        }
      });
    },

    //用户管理新增
    third_add(formdata_add) {
      this.$refs[formdata_add].validate(valid => {
        if (valid) {
          let lt = [];
          lt.push(this.formdata_add_role);
          this.formdata_add.role = lt;
          if(this.is_manager){
             this.formdata_add.is_manager=1
          }else{
             this.formdata_add.is_manager=0
          }
          users(this.formdata_add)
            .then(res => {
              this.formdata_add_role = "";
              this.third_list();
              this.$refs.formdata_add.resetFields();
              this.dialogVisible = false;
              this.$message({
                type: "success",
                message: res.msg
              });
            })
            .catch(err => {
              this.$message.error(err.msg);
            });
        } else {
          this.$message.error("失败");
        }
      });
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    deletes() {
      if (this.currentRow.name === "") {
        this.$message({
          message: "请先选择要修改的用户",
          type: "warning"
        });
      } else {
        this.$confirm("是否删" + this.currentRow.name + "用户?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            var datas = {
              operation: "DEL",
              id: this.currentRow.id
            };
            users(datas)
              .then(res => {
                this.third_list();
                this.$message({
                  type: "success",
                  message: res.msg
                });
              })
              .catch(err => {
                this.$message({
                  message: res.msg,
                  type: "warning"
                });
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      }
    }
  }
};
</script>
<style>
.only .flex_just {
  display: flex;
  justify-content: space-between;
}
.only .el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  /* line-height: 200px; */
}

.only .el-main {
  background-color: #e9eef3;
  color: #333;
}
.only .el-menu {
  border: solid 1px #e6e6e6;
  /* margin-bottom: 20px; */
}
.only .el-dialog__body {
  padding-bottom: 0px;
}
</style>
